/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package consum.bean;

import com.sun.jersey.core.util.MultivaluedMapImpl;
import java.awt.event.ActionEvent;
import java.io.Serializable;
import java.io.StringReader;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import trial.entity.Item;
import trial.util.itemSet;
import webservice.restful.client2.restfulClient2;

/**
 *
 * @author feifei
 */
@ManagedBean
@ViewScoped
public class ItemManagedBean implements Serializable{
   
    private String itemName;
    private Long id;
    private List<Item> allItems;

    public ItemManagedBean() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getItemName() {
        return itemName;
    }

    public void setItemName(String itemName) {
        this.itemName = itemName;
    }

    public void setAllItems(List<Item> allItems) {
        this.allItems = allItems;
    }
    
    
    public void submitItem(ActionEvent event)
    {
         MultivaluedMap formData = new MultivaluedMapImpl();
         formData.add("itemname", this.itemName);
         restfulClient2 client= new restfulClient2();
         String response=client.createItem(String.class, formData);
         System.out.println("userManagedBean Test*********************"+response);
         client.close();
    
    }
    public List<Item> getAllItems() throws JAXBException{
      restfulClient2 client2 =new restfulClient2();
      String xml=client2.getAllCurrentItems(MediaType.APPLICATION_XML.getClass(),null);
         if(!xml.isEmpty())
         {
        StringReader stringReader = new StringReader(xml);
        JAXBContext jc = JAXBContext.newInstance(itemSet.class); 
        itemSet productSet = (itemSet)jc.createUnmarshaller().unmarshal(stringReader);
        return productSet.getItems();
         }
         return null;
    }
    
}
